import Taro ,{useReachBottom }from '@tarojs/taro'
import { useEffect,useState } from 'react'
import { View,Text} from '@tarojs/components'
import {NewsSwiper,PostCard,ChooseBUttons,Nofinish} from '../../components'
import{getSuggestPositionBySal, getSuggestPositionByTime,} from '../../api/mode'
import './index.scss'


export default function Index()  {
const openid=Taro.getStorageSync(`openid`)
const [userInfo,setUserInfo] =useState([])
  //分页
  const [pageNum,setPageNum]=useState(1)

  //标识是否已经没有分页数据了
  const [flag,setFlag]=useState(false)

//  排序是按时间还是按薪资
  const[byWhat,setByWhat]=useState('byTime')
const auth=Taro.getStorageSync(`auth`);

useReachBottom(()=>{
    console.log('index reach bottom!');
    let snum=pageNum
    setPageNum(snum+1)
    console.log(snum);
})
// useEffect(()=>{
//   if(!auth){
//     console.log("未登录，去登录..");
//       Taro.reLaunch({
//         url: `../login/login`
//       });
//   }
// })

//初始化数据
useEffect(()=>{
  const params={
    // openid:openid,
    page:1,
    size:5,
   }
   if(byWhat=='byTime'){
    if(auth){
      getSuggestPositionByTime(openid,params).then(res=>{
        // console.log(res.data.data.records)
        setUserInfo(res.data.data.records)
      })
     }else{
      getSuggestPositionByTime(0,params).then(res=>{
        // console.log(res.data.data.records)
        setUserInfo(res.data.data.records)
      })
     }
   }else if(byWhat=='bySal'){
    getSuggestPositionBySal(openid,params).then(res=>{
      // console.log(res.data.data.records)
      setUserInfo(res.data.data.records)
    })
   }


},[byWhat])


//分页获取数据
useEffect(()=>{
  const params={
    // openid:openid,
    page:pageNum,
    size:5,
   }
   //未登录情况下的首页推荐获取
   if(!auth&&pageNum>1&&!flag&&byWhat=='byTime'){
    console.log('按时间排序');
   getSuggestPositionByTime(0,params).then(res=>{
    if(!res.data.data.records.length){
      setFlag(true)
      Taro.showToast({
        title:'没有更多职位了',
        icon:'error',
        duration:2000
      })
    }
     setUserInfo([
      ...userInfo,
      ...res.data.data.records
     ])
   })
   }

//登录下的首页情况获取
  if(auth&&pageNum>1&&!flag&&byWhat=='byTime'){
    console.log('按时间排序');
   getSuggestPositionByTime(openid,params).then(res=>{
    if(!res.data.data.records.length){
      setFlag(true)
      Taro.showToast({
        title:'没有更多职位了',
        icon:'error',
        duration:2000
      })
    }
     setUserInfo([
      ...userInfo,
      ...res.data.data.records
     ])

   })
  }else if(auth&&pageNum>1&&!flag&&byWhat=='bySal'){
    console.log('按薪资排序');
    getSuggestPositionBySal(openid,params).then(res=>{
      if(!res.data.data.records.length){
        setFlag(true)
        Taro.showToast({
          title:'没有更多职位了',
          icon:'error',
          duration:2000
        })
      }
       setUserInfo([
        ...userInfo,
        ...res.data.data.records
       ])
  
     })
  }
 
 },[pageNum])


  
const getUserValue=(val)=>{
  setByWhat(val)
console.log(val);
}
  
const setPage=()=>{
  setPageNum(1);
  setFlag(false)
console.log("change value");
}

const showMessage=()=>{   
    return(
      <View className='noSuggestCard' >
              <View className='noSuggestCardcontent'>
               <View>没有符合您期望的职位，</View> 
               <View>您可以修改您的期望职位条件。</View> 
              </View>  
              </View> 
    )
  
}

    return (

      <View className='index'>
      <NewsSwiper />
      
      <ChooseBUttons userValue={getUserValue} setPage={setPage} openid={openid} />

      <View>
      {userInfo
      ?
      userInfo.map((Info,index)=>(<PostCard key={index} Info={Info} canJump isCollected={Info.collected}  ></PostCard>))
      :
        null
      }
      
{/* 无数据时展示提示 */}
      {userInfo.length==0?showMessage():null}

      </View>

      </View>
    )
 
}
